﻿#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main() {
    string filename = "C:\\Users\\lrmwy\\source\\repos\\lab11.2\\ceshi.txt"; 
  
    vector<string> lines;
    ifstream inFile(filename, ios::binary);
    if (!inFile) {
        cerr << "无法打开输入文件！" << endl;
        return 1;
    }
    string line;
    while (getline(inFile, line, '\n')) { 
        if (!line.empty() && line.back() == '\r') {
            line.pop_back();
        }
        lines.push_back(line);
    }
    inFile.close();
    ofstream outFile(filename, ios::binary | ios::trunc);
    if (!outFile) {
        cerr << "无法打开输出文件！" << endl;
        return 1;
    }
    for (size_t i = 0; i < lines.size(); ++i) {
        outFile << (i + 1) << " " << lines[i] << '\n';
    }
    outFile.close();
    cout << "已为所有行添加行号！" << endl;
    return 0;
}